package br.com.chat.utils;

import java.util.ArrayList;
import java.util.List;

import br.com.buildup.cripto.Cryptography;
import br.com.buildup.cripto.CryptographyBuilder;
import br.com.buildup.cripto.CryptographyType;
import br.com.chat.Message;
import br.com.chat.User;

public class CryptographyMessage {
    
    private static final Cryptography crypto = CryptographyBuilder.build(CryptographyType.TRIPLE_DES);
    
    public static Message encrypt( Message message ) throws Exception{
        Message result = new Message(new User(crypto.encrypt(message.getFrom().getName())), 
                                     new User(crypto.encrypt(message.getTo().getName())));
        
        result.setContent(crypto.encrypt(message.getContent().toString()));
        
        return result;
    }
    
    public static Message decrypt(Message message) throws Exception {
        Message result = new Message(new User(crypto.decrypt(message.getFrom().getName())), 
                                     new User(crypto.decrypt(message.getTo().getName())));

        result.setContent(crypto.decrypt(message.getContent().toString()));

        return result;
    }
    
    public static List<Message> decrypt(List<Message> messages) throws Exception{
        List<Message> result = new ArrayList<Message>();
        
        for( Message message : messages ){
            result.add(decrypt(message));
        }
        
        return result;
    }
}
